package dao;

import entity.Group;
import entity.User;
import groupInformation.Invivation;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class InvitateDAO {

    public static boolean HasUser(User user) throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        boolean hasUser = false;//判断是否有该用户

//搜索用户id是否存在
        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");

            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾
            ps = connection.prepareStatement("select * from Group_User where id = ?");
            ps.setObject(1, user.getId());

            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中
            resultSet = ps.executeQuery();

            hasUser = resultSet.next();

//            System.out.println(hasUser);
//            int id = resultSet.getInt("id");
//            String account = resultSet.getString("account");
//            String password = resultSet.getString("password");
//            String nickname = resultSet.getString("nickname");
//            String profile = resultSet.getString("profile");
//            String gender = resultSet.getString("gender");
//            String create_time = resultSet.getString("create_time");
//            String level = resultSet.getString("level");
//            int age = resultSet.getInt("age");
//            String tag = resultSet.getString("tag");
//
//            System.out.println(id+","+account+","+nickname+","+password+","+profile+","+gender+","+create_time+","+level+","+age+","+tag);

        } catch (
                SQLException e) {
            e.printStackTrace();
        } finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet != null) {
                resultSet.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        return hasUser;
    }


    public static boolean InsertInvitate(int inviter_id, String invitee_id, String content, String inviterName) throws SQLException {

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");

            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾
            ps = connection.prepareStatement("insert into Group_invitation (inviter,invitee,content,inviterName) values(?,?,?,?) ");
//            ps.setObject(1, user.getId());
            ps.setObject(1, inviter_id);
            ps.setObject(2, invitee_id);
            ps.setObject(3, content);
            ps.setObject(4, inviterName);
            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中
            boolean n = ps.execute();


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet != null) {
                resultSet.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        return true;
    }


    /**
     * 查询邀请
     * author 风
     */
    public static List<Invivation> selectInvi(User user){
        List<Invivation> list = null;

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        final String URL = "jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC";
        final String USER = "root";
        final String PWD = "123456";

        try{
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection(URL, USER, PWD);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            String sql = "select * from Group_invitation where invitee = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getId());

            rs = pstmt.executeQuery();

            if (rs != null){
                list = new ArrayList<Invivation>();

                while (rs.next()){
                    Invivation inviv = new Invivation();




                    inviv.setGroupName(rs.getString("groupName"));
                    inviv.setGroupId(rs.getInt("groupId"));
                    inviv.setInviter(rs.getInt("inviter"));
                    inviv.setInviterName(rs.getString("inviterName"));
                    inviv.setContent(rs.getString("content"));

                    list.add(inviv);
                }

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if (pstmt != null){
                    pstmt.close();
                }
                if (conn != null){
                    conn.close();
                }
                if (rs != null){
                    conn.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        return list;
    }

}
